package com.lz.ttapi;

import android.content.Context;
import android.util.Log;
import android.view.View;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAppDownloadListener;
import com.bytedance.sdk.openadsdk.TTFullScreenVideoAd;
import com.lz.uikit.Constants;

import org.cocos2dx.javascript.AppActivity;
import org.cocos2dx.lib.Cocos2dxJavascriptJavaBridge;

public class TTGameFullScreenVideoAd {
    private  static final String TAG = "TTGameFullScreenVideoAd";
    private static TTAdNative mTTAdNative;
    private static TTFullScreenVideoAd mttFullVideoAd;
    private static boolean mIsExpress = false; //是否请求模板广告
    private static AppActivity app;
    private static boolean mHasShowDownloadActive = false;
    //视频广告发送奖励
    private static void sendReward(final int code){
        app.runOnGLThread(new Runnable() {
            @Override
            public void run() {
                //自己游戏奖励
                Cocos2dxJavascriptJavaBridge.evalString(String.format("SDKMgr.androidVideoFinish(%s)",String.valueOf( code )));
            }
        });
    }
    public static void init(Context activity){
        app = (AppActivity) activity;
        //step1:初始化sdk
        TTAdManager ttAdManager = TTAdManagerHolder.get();
        //step2:(可选，强烈建议在合适的时机调用):申请部分权限，如read_phone_state,防止获取不了imei时候，下载类广告没有填充的问题。
        TTAdManagerHolder.get().requestPermissionIfNecessary(app);
        //step3:创建TTAdNative对象,用于调用广告请求接口
        mTTAdNative = ttAdManager.createAdNative(app.getApplicationContext());
        onLoadAD();
    }

    public static void loadAd(String codeId, int orientation) {
        //step4:创建广告请求参数AdSlot,具体参数含义参考文档
        AdSlot adSlot;
        if (mIsExpress) {
            adSlot = new AdSlot.Builder()
                    .setCodeId(codeId)
                    //模板广告需要设置期望个性化模板广告的大小,单位dp,全屏视频场景，只要设置的值大于0即可
                    .setExpressViewAcceptedSize(0,0)
                    .setSupportDeepLink(true)
                    .setOrientation(orientation)//必填参数，期望视频的播放方向：TTAdConstant.HORIZONTAL 或 TTAdConstant.VERTICAL
                    .build();

        } else {
            adSlot = new AdSlot.Builder()
                    .setCodeId(codeId)
                    .setSupportDeepLink(true)
                    .setOrientation(orientation)//必填参数，期望视频的播放方向：TTAdConstant.HORIZONTAL 或 TTAdConstant.VERTICAL
                    .build();
        }
        //step5:请求广告
        mTTAdNative.loadFullScreenVideoAd(adSlot, new TTAdNative.FullScreenVideoAdListener() {
            @Override
            public void onError(int code, String message) {
                Log.e(TAG, "onError: " + code + ", " + String.valueOf(message));
                //TToast.show(FullScreenVideoActivity.this, message);
                sendReward(2);
            }

            @Override
            public void onFullScreenVideoAdLoad(TTFullScreenVideoAd ad) {
                Log.e(TAG, "onFullScreenVideoAdLoad");

                //TToast.show(FullScreenVideoActivity.this, "FullVideoAd loaded  广告类型：" + getAdType(ad.getFullVideoAdType()));
                mttFullVideoAd = ad;
                mttFullVideoAd.setFullScreenVideoAdInteractionListener(new TTFullScreenVideoAd.FullScreenVideoAdInteractionListener() {

                    @Override
                    public void onAdShow() {
                        //TToast.show(FullScreenVideoActivity.this, "FullVideoAd show");
                    }

                    @Override
                    public void onAdVideoBarClick() {
                       // TToast.show(FullScreenVideoActivity.this, "FullVideoAd bar click");
                    }

                    @Override
                    public void onAdClose() {
                        //TToast.show(FullScreenVideoActivity.this, "FullVideoAd close");
                        sendReward(1);
                    }

                    @Override
                    public void onVideoComplete() {
                       // TToast.show(FullScreenVideoActivity.this, "FullVideoAd complete");
                        sendReward(1);
                    }

                    @Override
                    public void onSkippedVideo() {
                       // TToast.show("FullVideoAd skipped");
                        sendReward(1);
                    }

                });


                ad.setDownloadListener(new TTAppDownloadListener() {
                    @Override
                    public void onIdle() {
                        mHasShowDownloadActive = false;
                    }

                    @Override
                    public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) {
                        Log.d("DML", "onDownloadActive==totalBytes=" + totalBytes + ",currBytes=" + currBytes + ",fileName=" + fileName + ",appName=" + appName);

                        if (!mHasShowDownloadActive) {
                            mHasShowDownloadActive = true;

                        }
                    }

                    @Override
                    public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) {
                        Log.d("DML", "onDownloadPaused===totalBytes=" + totalBytes + ",currBytes=" + currBytes + ",fileName=" + fileName + ",appName=" + appName);

                    }

                    @Override
                    public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) {
                        Log.d("DML", "onDownloadFailed==totalBytes=" + totalBytes + ",currBytes=" + currBytes + ",fileName=" + fileName + ",appName=" + appName);

                    }

                    @Override
                    public void onDownloadFinished(long totalBytes, String fileName, String appName) {
                        Log.d("DML", "onDownloadFinished==totalBytes=" + totalBytes + ",fileName=" + fileName + ",appName=" + appName);

                    }

                    @Override
                    public void onInstalled(String fileName, String appName) {
                        Log.d("DML", "onInstalled==" + ",fileName=" + fileName + ",appName=" + appName);
                    }
                });
            }

            @Override
            public void onFullScreenVideoCached() {
                Log.e(TAG, "onFullScreenVideoCached");
            }
        });
    }


    public static  void onLoadAD(){
        loadAd(Constants.TT_FULLSCREEN_ID, TTAdConstant.VERTICAL);
    }

    public static void  playFullScrennVide(){
        if (mttFullVideoAd != null) {
            //step6:在获取到广告后展示
            //该方法直接展示广告
            //mttFullVideoAd.showFullScreenVideoAd(FullScreenVideoActivity.this);
            //展示广告，并传入广告展示的场景
            //mttFullVideoAd.showFullScreenVideoAd(app);
            app.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mttFullVideoAd.showFullScreenVideoAd(app, TTAdConstant.RitScenes.GAME_GIFT_BONUS, null);
                    mttFullVideoAd = null;
                }
            } );

        } else {
            loadAd(Constants.TT_FULLSCREEN_ID, TTAdConstant.VERTICAL);
        }
    }
}
